<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="vipsearch()" size="small">查询搜索</el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label>
            <el-select v-model="formInline.region" placeholder="搜索类型">
              <el-option label="会员姓名" value="name"></el-option>
              <el-option label="会员id" value="id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="搜索内容">
            <el-input v-model="formInline.text" placeholder="请输入搜索内容"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="addvip()" style="float:right">新增会员</el-button>
    </el-card>
    <div class="table-container">
      <el-table :data="getvipfydata" style="width: 100%;" border>
        <el-table-column label="会员姓名" width="150" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="会员ID" align="center">
          <template slot-scope="scope">{{scope.row.member.userId}}</template>
        </el-table-column>
        <el-table-column label="会员卡号" width="160" align="center">
          <template slot-scope="scope">{{scope.row.member.number}}</template>
        </el-table-column>
        <el-table-column label="会员积分" width="160" align="center">
          <template slot-scope="scope">{{scope.row.member.score}}</template>
        </el-table-column>
        <el-table-column label="会员折扣" width="160" align="center">
          <template slot-scope="scope">{{scope.row.member.discount}}</template>
        </el-table-column>
        <el-table-column label="卡上余额" width="160" align="center">
          <template slot-scope="scope">{{scope.row.member.money}}</template>
        </el-table-column>
        <el-table-column label="会员等级" width="160" align="center">
          <template slot-scope="scope">{{scope.row.member.level}}</template>
        </el-table-column>
        <el-table-column label="会员状态" width="160" align="center">
          <template slot-scope="scope">{{scope.row.member.state}}</template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑信息</el-button>
            <el-button size="mini" type="primary" @click="recharge(scope.$index, scope.row)">余额充值</el-button>
            <el-button
              v-if="scope.row.member.state=='正常'"
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >会员冻结</el-button>
            <el-button
              v-else-if="scope.row.member.state=='冻结'"
              size="mini"
              type="danger"
              @click="refreeze(scope.$index, scope.row)"
            >解除冻结</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <!-- 
            background---设置背景颜色
            hide-on-single-page---只有一页时隐藏分页
            :total---总数据数量
            :page-size---每页显示数据的条数
            :pager-count---最大可见页码数
            @current-change---currentPage改变时会触发(会自动获得页码)
            :current-page---当前页
      -->
      <el-pagination
        background
        hide-on-single-page
        layout="prev, pager, next"
        :total="pageNum"
        :page-size="pageSize"
        :pager-count="pageCount"
        @current-change="changePage"
        :current-page="currentPage"
      ></el-pagination>
    </div>
    <!-- Form -->
    <el-dialog title="请输入会员信息" :visible.sync="dialogFormVisible">
      <el-form :model="vipform" :rules="rules" ref="vipform">
        <el-form-item label="会员id" :label-width="formLabelWidth" prop="userId">
          <el-input v-model="vipform.userId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="vipform.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员卡号" :label-width="formLabelWidth" prop="number">
          <el-input v-model="vipform.number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="vipform.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="充值金额" :label-width="formLabelWidth" prop="money">
          <el-input v-model="vipform.money" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员性别" :label-width="formLabelWidth">
          <el-select v-model="vipform.sex" placeholder="请选择">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="vipform.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="身份证" :label-width="formLabelWidth">
          <el-input v-model="vipform.idCard" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button :disabled="disableds" type="primary" @click="addvipbt(vipform)">确 定</el-button>
      </div>
    </el-dialog>
    <!-- Form -->
    <el-dialog title="请输入充值信息" :visible.sync="rechargeformVisible">
      <el-form :model="rechargeform">
        <el-form-item label="支付方式" :label-width="formLabelWidth">
          <el-select v-model="rechargeform.payment" placeholder="请选择充值方式">
            <el-option label="现金支付" value="现金支付"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="充值金额" :label-width="formLabelWidth">
          <el-input v-model="rechargeform.money" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="rechargeformVisible = false">取 消</el-button>
        <el-button type="primary" @click="rechargebt()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- Form -->
    <el-dialog title="请编辑会员信息" :visible.sync="editFormVisible">
      <el-form :model="editform">
        <el-form-item label="会员ID" :label-width="formLabelWidth">
          <el-input v-model="editform.id" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="会员账号" :label-width="formLabelWidth">
          <el-input v-model="editform.number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员折扣" :label-width="formLabelWidth">
          <el-input v-model="editform.discount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员状态" :label-width="formLabelWidth">
          <el-select v-model="editform.state" placeholder="请选择">
            <el-option label="正常" value="0"></el-option>
            <el-option label="冻结" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员等级" :label-width="formLabelWidth">
          <el-select v-model="editform.level" placeholder="请选择">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editvipbt()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  name: "hySet",
  computed: {
    getvipfydata() {
      let qishi = (this.currentPage - 1) * 5;
      let jieshu = (this.currentPage - 1) * 5 + 5;
      this.$store.state.bsStore.vipData.forEach(itme => {
        itme.member.state == 0
          ? (itme.member.state = "正常")
          : (itme.member.state = "冻结");
        itme.sex == 1 ? (itme.sex = "男") : (itme.sex = "女");
        itme.createTime = new Date(itme.createTime).toLocaleString();
        itme.updateTime = new Date(itme.updateTime).toLocaleString();
        itme.loginTime = new Date(itme.loginTime).toLocaleString();
      });
      return this.$store.state.bsStore.vipData.slice(qishi, jieshu);
      // console.log(this.$store.state.bsStore.vipData);
    },
    pageNum() {
      return this.$store.state.bsStore.vipData.length;
    }
  },
  data() {
    return {
      //查询
      formInline: {
        text: "",
        region: ""
      },
      //会员
      vipform: {
        name: "",
        password: "",
        sex: "",
        tel: "",
        idCard: "",
        state: "0",
        score: "0",
        discount: "9.5",
        level: "1",
        money: "",
        userId: "",
        number: ""
      },
      //充值
      rechargeform: {
        userId: "",
        money: ""
      },
      //会员编辑
      editform: {
        id: "",
        number: "",
        state: "",
        discount: "",
        level: "",
        updateTime: ""
      },
      editFormVisible: false,
      rechargeformVisible: false,
      formLabelWidth: "120px",
      listLoading: false,
      dialogFormVisible: false,
      disableds: false,

      //分页
      currentPage: 1, //当前页
      pageSize: 5, //显示多少条
      pageCount: 7, //页码最大显示多少数
      //添加会员
      rules: {
        number: [
          { required: true, message: "输入的卡号不能为空", trigger: "blur" },
          { validator: this.validVipnum, trigger: "blur", required: true }
        ],
        name: [
          { required: true, message: "输入的用户名不能为空", trigger: "blur" },
          { pattern: /^[\u4E00-\u9FA5]+$/, message: "用户名只能为中文" }
        ],
        userId: [
          { required: true, message: "输入的ID不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "输入的密码不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    ...mapActions([
      "getvipData",
      "addvipData",
      "userRecharge",
      "editvipData",
      "vipsearchData",
      "freezeVip"
    ]),
    //添加会员M
    addvip() {
      // console.log("弹出餐桌模态框");
      this.dialogFormVisible = true; //弹出模态框
      let date = new Date();
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let minute = date.getMinutes();
      let second = date.getSeconds();
      minute = minute < 10 ? "0" + minute : minute;
      second = second < 10 ? "0" + second : second;
      this.vipform.createTime = y + "-" + m + "-" + d;
    },
    //添加会员BTN
    addvipbt(vipform) {
      console.log(vipform);
      // this.$store.state.bsStore.vipData.forEach(item => {});
      // this.addvipData()
      this.dialogFormVisible = false;

      this.addvipData(this.vipform).then(res => {
        if (res === 200) {
          this.$message({
            type: "success",
            message: "添加成功!"
          });
        } else {
          this.$message({
            type: "info",
            message: "添加失败!"
          });
        }
      });
    },
    //重置搜索
    handleResetSearch() {
      this.formInline.text = "";
      this.formInline.region = "";
      this.getvipData();
    },
    //会员冻结BTN
    handleDelete(index, row) {
      let obj = {};
      (obj.id = row.id), (obj.state = row.member.state == "正常" ? 1 : 0);
      this.$confirm("此操作将冻结该会员, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
      .then(() => {
          this.freezeVip(obj).then(res => {
            if (res === 200) {
              this.getvipData()
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "操作失败"
          });
        });
    },
    //会员解冻
    refreeze(index, row) {
      let obj = {};
      (obj.id = row.id), (obj.state = row.member.state == "冻结" ? 0 : 1);
      this.$confirm("此操作将解除冻结, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.freezeVip(obj).then(res => {
            if (res === 200) {
              this.getvipData()
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "操作失败"
          });
        });
    },
    //会员编辑m
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editform.id = JSON.parse(JSON.stringify(row.id));
      this.editform.number = JSON.parse(JSON.stringify(row.member.number));
      this.editform.state = JSON.parse(JSON.stringify(row.member.state));
      this.editform.level = JSON.parse(JSON.stringify(row.member.level));
      this.editform.discount = JSON.parse(JSON.stringify(row.member.discount));
      this.editform.state == "正常" ? 0 : 1;
      let date = new Date();
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let minute = date.getMinutes();
      let second = date.getSeconds();
      minute = minute < 10 ? "0" + minute : minute;
      second = second < 10 ? "0" + second : second;
      this.editform.updateTime = y + "-" + m + "-" + d;
    },
    //分页目标页
    changePage(val) {
      this.currentPage = val;
    },
    //余额充值M
    recharge(index, row) {
      this.rechargeformVisible = true;
      // if (row.member.state == "正常") {
      //   this.rechargeform.state = 0;
      // } else if (row.member.state == "冻结") {
      //   this.rechargeform.state = 1;
      // } else if (row.member.state == "挂失") {
      //   this.rechargeform.state = 2;
      // }
      this.rechargeform.userId = parseInt(row.id);
    },
    //充值BTN
    rechargebt() {
      this.rechargeformVisible = false;
      this.userRecharge(this.rechargeform).then(res => {
        if (res === 200) {
          this.getvipData();
          this.$message({
            type: "success",
            message: "充值成功!"
          });
        } else {
          this.$message({
            type: "info",
            message: "充值失败!"
          });
        }
      });
    },
    //会员搜索
    vipsearch() {
      this.vipsearchData(this.formInline).then(res => {
        if (res === 200) {
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        } else {
          this.$message({
            type: "info",
            message: "查询失败宁输入的信息可能有误!"
          });
        }
      });
    },
    //编辑会员信息BTN
    editvipbt() {
      this.editFormVisible = false;
      this.editvipData(this.editform).then(res => {
        if (res === 200) {
          this.getvipData();
          this.$message({
            type: "success",
            message: "修改成功!"
          });
        } else {
          this.$message({
            type: "info",
            message: "修改失败!"
          });
        }
      });
    },
    numberweiyi(row) {
      this.$store.state.bsStore.vipData.forEach(item => {
        item.number == row;
        return false;
      });
      console.log(row);
    },
    //会员验证
    async validVipnum(rule, value, callback) {
      if (!value) {
        callback(new Error("请输入您的会员id"));
      } else {
        this.$store.state.bsStore.vipData.forEach(item => {
          if (item.member.number == value) {
            this.disableds = true;
            callback(new Error("该ID已被占用"));
          } else {
            callback();
          }
        });
      }
    }
  },
  //请求会员数据
  created() {
    this.getvipData();
  }
};
</script>

<style scoped>
.operate-container {
  margin: 15px 0;
}
.el-input {
  width: 300px;
}
.block {
  margin-left: 50%;
}
</style>